//$Id:CashFlowAction.java 2009-12-31 下午09:41:14
package com.walllee.module.pay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.walllee.core.dao.Page;
import com.walllee.core.meta.TableMeta;
import com.walllee.core.meta.TableMetaDAO;
import com.walllee.core.util.PageUtil;
import com.walllee.core.util.json.DateJsonValueProcessorImpl;
import com.walllee.core.util.json.TimeStampJsonValueProcessorImpl;

/**
 * 
 *
 * @author Wall.Lee(xiaoshan2242@gmail.com)
 */
public class CashFlowAction extends  ActionSupport implements ModelDriven {
	private static final long serialVersionUID = -6424026146765437567L;
	private String module="cashFlow" ;
	private CashFlowDAO dao ;
	private TableMetaDAO metaDao ;
	private List<TableMeta> tableMeta = new ArrayList<TableMeta>();
	@SuppressWarnings("unchecked")
	private List<Map> datas = new ArrayList<Map>();
	private CashFlow entity = new CashFlow();
	private String jsonData = "" ;
	private int start ;
	private int limit ;
	private String deleteList = "" ;
	
	public String list() throws Exception{
		datas = dao.getAllCashFlow() ;
		tableMeta = metaDao.getTableMeta("op_cash_flow", null ) ;
		System.out.println("vm");
		return "vm" ;
	}
	
	/**
	 * 获取grid中的数据
	 * @return
	 * @throws Exception
	 */
	public String gridlist() throws Exception{
		int page = PageUtil.index2Page( start , limit ) ;
		Page pageInfo = dao.queryPage(page, limit ) ;
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.sql.Date.class,new DateJsonValueProcessorImpl());
		config.registerJsonValueProcessor(java.sql.Timestamp.class,new TimeStampJsonValueProcessorImpl());
		HashMap<String , Object> map = new HashMap<String , Object>();
		map.put("totalCount", pageInfo.getTotalCount());
		map.put("data", pageInfo.getItems());
		JSONObject json = JSONObject.fromObject(map , config);
		jsonData = json.toString() ;
		return "jsonvm" ;
	}
	
	public String add() throws Exception{
		String flowId = entity.getFlowId();
		if ( flowId == null || "".equals(flowId)) {
			jsonData = "{success:false}" ;
		}else{
			int result = dao.addEntity(entity);
			jsonData = "{success:true,data:{result:'"+ result +"'}}";
		}
		return "jsonvm" ;
	}
	
	public String delete() throws Exception{
		if ( deleteList ==null || "".equals(deleteList)){
			jsonData = "{success:false}" ;
		}else{
			String[] ids = deleteList.split(",");
			dao.delete(ids);
			jsonData = "{success:true}" ;
		}
		return "jsonvm" ;
	}
	
	public void setDao(CashFlowDAO dao) {
		this.dao = dao;
	}

	public void setMetaDao(TableMetaDAO metaDao) {
		this.metaDao = metaDao;
	}
	
	public List<TableMeta> getTableMeta() {
		return tableMeta;
	}

	public List<Map> getDatas() {
		return datas;
	}
	
	public String getModule(){
		return this.module ;
	}

	@Override
	public Object getModel() {
		return entity ;
	}

	public CashFlow getEntity() {
		return entity;
	}

	public String getJsonData() {
		return jsonData;
	}

	public void setStart(int start) {
		this.start = start ;
	}

	public void  setLimit(int limit) {
		this.limit = limit ;
	}

	public void setDeleteList(String deleteList) {
		this.deleteList = deleteList;
	}

}
